package com.invest.controller.user;

import com.github.pagehelper.Page;
import com.invest.controller.base.BaseController;

import com.invest.domain.model.SystemUserDO;
import com.invest.infra.constants.AppConstants;
import com.invest.infra.mvc.utils.JsonResultTemplate;
import com.invest.infra.mvc.utils.PageVO;
import com.invest.repository.SysUserRepository;
import com.invest.utils.type.SystemUserRoleType;
import com.invest.utils.type.SystemUserStatusType;
import com.invest.vo.UserVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>注释</p>
 *
 * @author yuyizhao
 * @version v0.1 2016/3/9 Exp
 */
@Controller
@RequestMapping(value = "/admin")
public class UserController extends BaseController {

    @Autowired
    private SysUserRepository adminRepository;

    @RequestMapping(value="/home", method = RequestMethod.GET)
    public String home(Model modelMap){

        return "admin/home";
    }

    @RequestMapping(value="user/list", method = RequestMethod.GET)
    public String userList(Model modelMap){

        return "admin/user-list";
    }

    @RequestMapping(value="user/list", method = RequestMethod.POST)
    public String userListData(Model modelMap, SystemUserDO user, PageVO pageVO){

        Page<SystemUserDO> page = adminRepository.query(user,pageVO);
        modelMap.addAttribute("page",page);
        return "admin/user-list-data";
    }
    @RequestMapping(value="user/add", method = RequestMethod.GET)
    public String initCrp(Model modelMap){

        modelMap.addAttribute("adminRoles", SystemUserRoleType.getMsgMap());
        return "admin/user-add";
    }

    @RequestMapping(value="user/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(Model modelMap, final UserVO adminVO){
        return new JsonResultTemplate<UserVO,Boolean>(){
            @Override
            public Boolean doService(UserVO adminVO) {
                 adminRepository.save(adminVO,getOperator());
                return  true;
            }
        }.execute(adminVO);
    }

    @RequestMapping(value="user/pwd/change", method = RequestMethod.GET)
    public String changePwdInit(){
        return "admin/user-pwd-change";
    }

    @RequestMapping(value="user/pwd/change", method = RequestMethod.POST)
    @ResponseBody
    public String changePwd(String password){
        return new JsonResultTemplate<String,Boolean>(){
            @Override
            public Boolean doService(String password) {
                Assert.isTrue(StringUtils.isNotBlank(password),"密码不能为空");
                return adminRepository.changePwd(getOperator().getId(),password,getOperator().getId());
            }
        }.execute(password);
    }

    @RequestMapping(value="user/pwd/reset/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String resetPwd(@PathVariable final String  id){

        return new JsonResultTemplate<String,Boolean>(){
            @Override
            public Boolean doService(String id) {

                return adminRepository.changePwd(id, AppConstants.DEFAULT_PASSWORD,getOperator().getId());
            }
        }.execute(id);
    }

    @RequestMapping(value="user/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@PathVariable final String  id){

        return new JsonResultTemplate<String,Boolean>(){
            @Override
            public Boolean doService(String id) {

                 adminRepository.delete(id);
                return true;
            }
        }.execute(id);
    }
}

